//
//  SetVoltageViewController.m
//  CarCare
//
//  Created by ileo on 15/5/11.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "SetVoltageViewController.h"
#import "SettingMgr.h"
#import "FLabel.h"

@interface SetVoltageViewController ()

@property (nonatomic, strong) FLabel *currentVoltageL;
@property (nonatomic, strong) FLabel *minVoltageL;
@property (nonatomic, strong) UILabel *minVoltageTimeL;

@property (nonatomic, strong) UILabel *choiceVoltageL;

@property (nonatomic, strong) UIView *valueBG;

@property (nonatomic, strong) UISlider *sliderV;

@end

@implementation SetVoltageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CommentHelper useFunction:Use_Voltage];
    
    self.title = @"电压设置";
    self.view.backgroundColor = [Theme backGroundColor];
    //
    UIView *topBG = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 110)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:self.view];
    topBG.layer.borderColor = [Theme lineColor].CGColor;
    topBG.layer.borderWidth = 0.5;
    [Factory labelWithFrame:CGRectMake(0, 20, __MAIN_WIDTH/2, 20) font:FONT_DEFAULT_Light(12) text:@"当前电压" textColor:[Theme wordColor] onView:topBG textAlignment:NSTextAlignmentCenter];
    [Factory labelWithFrame:CGRectMake(__MAIN_WIDTH/2, 23, __MAIN_WIDTH/2, 20) font:FONT_DEFAULT_Light(12) text:@"历史最低电压" textColor:[Theme wordColor] onView:topBG textAlignment:NSTextAlignmentCenter];
    self.minVoltageTimeL = [Factory labelWithFrame:CGRectMake(__MAIN_WIDTH/2, 43, __MAIN_WIDTH/2, 20) font:FONT_DEFAULT_Light(12) text:@"0000-00-00" textColor:[[Theme wordColor] colorWithAlphaComponent:0.6] onView:topBG textAlignment:NSTextAlignmentCenter];
    FLabel *symbol1L = [[FLabel alloc] initWithMaxWidth:100 resetSizeFinish:nil];
    symbol1L.text = @"V";
    symbol1L.font = FONT_DEFAULT_Light(15);
    symbol1L.textColor = [Theme wordColor];
    [topBG addSubview:symbol1L];
    symbol1L.backgroundColor = [UIColor clearColor];
    self.currentVoltageL = [[FLabel alloc] initWithMaxWidth:100 resetSizeFinish:^(UILabel *label) {
        label.center = CGPointMake(__MAIN_WIDTH/4 - 10, topBG.height/3*2 + 5);
        symbol1L.bottom = label.bottom - 7;
        symbol1L.left = label.right + 5;
    }];
    self.currentVoltageL.font = FONT_DEFAULT_Light(40);
    self.currentVoltageL.text = @"0";
    self.currentVoltageL.textColor = [Theme redColor];
    self.currentVoltageL.backgroundColor = [UIColor clearColor];
    [topBG addSubview:self.currentVoltageL];
    FLabel *symbol2L = [[FLabel alloc] initWithMaxWidth:100 resetSizeFinish:nil];
    symbol2L.text = @"V";
    symbol2L.font = FONT_DEFAULT_Light(15);
    symbol2L.textColor = [Theme wordColor];
    [topBG addSubview:symbol2L];
    symbol2L.backgroundColor = [UIColor clearColor];
    self.minVoltageL = [[FLabel alloc] initWithMaxWidth:100 resetSizeFinish:^(UILabel *label) {
        label.center = CGPointMake(__MAIN_WIDTH/4*3 - 10, topBG.height/4*3 + 2);
        symbol2L.bottom = label.bottom - 3;
        symbol2L.left = label.right + 4;
    }];
    self.minVoltageL.font = FONT_DEFAULT_Light(25);
    self.minVoltageL.text = @"0";
    [topBG addSubview:self.minVoltageL];
    
    
    //
    UIView *switchBG = [[[UIView viewWithFrame:CGRectMake(0, 125, __MAIN_WIDTH, 44)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:self.view];
    switchBG.layer.borderColor = [Theme lineColor].CGColor;
    switchBG.layer.borderWidth = 0.5;
    [Factory labelWithFrame:CGRectMake(20, 0, 100, switchBG.height) font:FONT_DEFAULT_Light(14) text:@"低电压提醒" textColor:[Theme wordColor] onView:switchBG textAlignment:NSTextAlignmentLeft];
    UISwitch *volSwitch = [[UISwitch alloc] init];
    volSwitch.center = CGPointMake(switchBG.width - 45, switchBG.height/2);
    volSwitch.on = SETTINGMGR.showTipsVoltage;
    [volSwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
    volSwitch.onTintColor = [Theme mainColor];
    [switchBG addSubview:volSwitch];
    
    
    
    //
    self.valueBG = [[[UIView viewWithFrame:CGRectMake(0, switchBG.bottom, __MAIN_WIDTH, 150)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:self.view];
    self.valueBG.layer.borderColor = [Theme lineColor].CGColor;
    self.valueBG.layer.borderWidth = 0.5;
    self.valueBG.hidden = !volSwitch.on;

    self.choiceVoltageL = [Factory labelWithFrame:CGRectMake(20, 15, 200, 30) font:FONT_DEFAULT_Light(14) text:[NSString stringWithFormat:@"报警电压值：%0.1f",SETTINGMGR.voltageValue] textColor:[Theme wordColor] onView:self.valueBG textAlignment:NSTextAlignmentLeft];
    
    CGFloat gap = 30;
    self.sliderV = [[UISlider alloc] initWithFrame:CGRectMake(gap - 10, self.valueBG.height - 68, __MAIN_WIDTH - 2 *gap + 20, 50)];
    self.sliderV.minimumValue = 10;
    self.sliderV.maximumValue = 15;
//    self.sliderV.continuous = NO;
    self.sliderV.minimumTrackTintColor = [Theme mainColor];
    self.sliderV.value = SETTINGMGR.voltageValue;
    [self.sliderV addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
    [self.sliderV addTarget:self action:@selector(sliderChangeOver:) forControlEvents:UIControlEventTouchUpInside];
    [self.valueBG addSubview:self.sliderV];
    
    CGFloat width = (__MAIN_WIDTH - 2 * gap)/5;
    UIColor *lightcolor = [[Theme wordColor] colorWithAlphaComponent:0.6];
    for (int i = 0; i < 6; i++) {
        UILabel *label = [Factory labelWithFrame:CGRectMake(0, self.choiceVoltageL.bottom + 10, 60, 20) font:FONT_DEFAULT_Light(11) text:[NSString stringWithFormat:@"%zdv",10+i] textColor:lightcolor onView:self.valueBG textAlignment:NSTextAlignmentCenter];
        label.centerX = gap + i *width;
        [[[UIView viewWithFrame:CGRectMake(gap + i * width, label.bottom + 5, 0.5, 10)] resetBackgroundColor:lightcolor] setupOnView:self.valueBG];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification) name:REFRESH_VOLTAGE object:nil];
    [self notification];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)switchChange:(UISwitch *)switchView{
    [[StatisticsMgr sharedInstance] addClickID:[NSString stringWithFormat:@"电压提醒－低电压提醒:%@",(switchView.on ? @"打开" : @"关闭")] description:switchView.description];
    self.valueBG.hidden = !switchView.on;
    [SETTINGMGR setTipsVoltage:switchView.on];
}

-(void)sliderChangeOver:(UISlider *)sliderView{
    [SETTINGMGR setShowVoltage:self.sliderV.value];
}

-(void)sliderChange:(UISlider *)sliderView{
    self.choiceVoltageL.text = [NSString stringWithFormat:@"报警电压值：%0.1f",sliderView.value];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [SETTINGMGR.refreshVoltage startRefreshWithTimeInterval:0];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SETTINGMGR.refreshVoltage stopRefresh];
}

-(void)notification{
    self.minVoltageTimeL.text = SETTINGMGR.refreshVoltage.minVoltageTime;
    self.minVoltageL.text = [NSString stringWithFormat:@"%0.1f",SETTINGMGR.refreshVoltage.minVoltage];
    self.currentVoltageL.text = [NSString stringWithFormat:@"%0.1f",SETTINGMGR.refreshVoltage.currentVoltage];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
